package com.test.cordova.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;
import android.util.Log;

import com.test.cordova.observer.NetStateObservable;


/**
 * 监听网络状态
 */

public class NetWorkStateReceiver extends BroadcastReceiver {

    private static final String TAG = NetWorkStateReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {

        //检测API是不是小于23，因为到了API23之后getNetworkInfo(int networkType)方法过时
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            Log.i(TAG, "net state change < 23");
            //获得ConnectivityManager对象
            ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
            if (activeNetworkInfo!=null && activeNetworkInfo.isConnected()) {
                //getTypeName   MOBILE 手机数据网路  WIFI  无线网路
                Log.e(TAG, activeNetworkInfo.getTypeName() + " 已连接");
                NetStateObservable.getInstance().notifyObservers(activeNetworkInfo.getTypeName());
            } else {
                //断开会执行2次
                NetStateObservable.getInstance().notifyObservers("网路已断开");
                Log.e(TAG, "网路已断开");
            }

            /*//获取ConnectivityManager对象对应的NetworkInfo对象
            //获取WIFI连接的信息
            NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            //获取移动数据连接的信息
            NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
                Toast.makeText(context, "WIFI已连接,移动数据已连接", Toast.LENGTH_SHORT).show();
            } else if (wifiNetworkInfo.isConnected() && !dataNetworkInfo.isConnected()) {
                Toast.makeText(context, "WIFI已连接,移动数据已断开", Toast.LENGTH_SHORT).show();
            } else if (!wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
                Toast.makeText(context, "WIFI已断开,移动数据已连接", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "WIFI已断开,移动数据已断开", Toast.LENGTH_SHORT).show();
            }*/

        } else {
            Log.i(TAG, "net state change  api >= 23");
            //API大于23时使用下面的方式进行网络监听
            ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            //获取所有网络连接的信息
            Network[] networks = connMgr.getAllNetworks();
            if (networks.length == 0) {
                Log.e(TAG,"网络已断开");
            } else if (networks.length == 1) {
                NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[0]);
                Log.e(TAG,"getTypeName:"+networkInfo.getTypeName()+" isConnected:"+networkInfo.isConnected()+"getType:"+networkInfo.getType());
                if (networkInfo.isConnected()) {
                    //getTypeName   MOBILE 手机数据网路  WIFI  无线网路
                    Log.e(TAG, networkInfo.getTypeName() + " 已连接");
                }
            }

        }

    }
}
